package com.we.flink.userlabelplatform.tag;

import org.apache.flink.api.common.functions.FilterFunction;
import org.apache.flink.api.common.functions.RichFlatMapFunction;
import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
import org.apache.flink.util.Collector;

import com.alibaba.fastjson.JSONObject;
import com.we.flink.userlabelplatform.table.TableIncrLimitRecord;

public class TagIncrLimitRecord {
    public long uid;
    public int last_education_increase_quota_time;

    public TagIncrLimitRecord() {}

    public TagIncrLimitRecord(long uid, int last_education_increase_quota_time) {
        this.uid = uid;
        this.last_education_increase_quota_time = last_education_increase_quota_time;
    }

    public static TagIncrLimitRecord init(long uid) {
        return new TagIncrLimitRecord(uid, 0);
    }

    public static SingleOutputStreamOperator<String> getFlatMapStream(
            StreamTableEnvironment tableEnv, StreamExecutionEnvironment env) {
        Table tableRes = tableEnv.sqlQuery(TableIncrLimitRecord.QUERYSQL);
        DataStream<Tuple2<Boolean, TableIncrLimitRecord>> inputStream =
                tableEnv.toRetractStream(tableRes, TableIncrLimitRecord.class);

        SingleOutputStreamOperator<String> out =
                inputStream
                        .filter(
                                new FilterFunction<Tuple2<Boolean, TableIncrLimitRecord>>() {
                                    @Override
                                    public boolean filter(
                                            Tuple2<Boolean, TableIncrLimitRecord> value)
                                            throws Exception {
                                        return value.f0;
                                    }
                                })
                        .uid("TagIncrLimitRecord-filter")
                        .keyBy(
                                new KeySelector<Tuple2<Boolean, TableIncrLimitRecord>, Long>() {
                                    @Override
                                    public Long getKey(Tuple2<Boolean, TableIncrLimitRecord> value)
                                            throws Exception {
                                        return value.f1.getUid();
                                    }
                                })
                        .flatMap(
                                new RichFlatMapFunction<
                                        Tuple2<Boolean, TableIncrLimitRecord>, String>() {
                                    ValueState<TagIncrLimitRecord> incrLimitRecordVState;

                                    @Override
                                    public void open(Configuration parameters) throws Exception {
                                        incrLimitRecordVState =
                                                getRuntimeContext()
                                                        .getState(
                                                                new ValueStateDescriptor<
                                                                        TagIncrLimitRecord>(
                                                                        "incrLimitRecordVState",
                                                                        TagIncrLimitRecord.class));
                                    }

                                    @Override
                                    public void flatMap(
                                            Tuple2<Boolean, TableIncrLimitRecord> value,
                                            Collector<String> out)
                                            throws Exception {
                                        long uid = value.f1.getUid();
                                        int update_time = value.f1.getUpdated_at();

                                        TagIncrLimitRecord tmpState = incrLimitRecordVState.value();
                                        TagIncrLimitRecord curState =
                                                tmpState == null
                                                        ? TagIncrLimitRecord.init(uid)
                                                        : tmpState;

                                        if (update_time
                                                > curState
                                                        .getLast_education_increase_quota_time()) {
                                            JSONObject jsonObject = new JSONObject();
                                            jsonObject.put("uid", uid);

                                            jsonObject.put(
                                                    "last_education_increase_quota_time",
                                                    update_time);
                                            curState.last_education_increase_quota_time =
                                                    update_time;

                                            out.collect(jsonObject.toString());
                                            incrLimitRecordVState.update(curState);
                                        }
                                    }
                                });
        return out;
    }

    public long getUid() {
        return uid;
    }

    public int getLast_education_increase_quota_time() {
        return last_education_increase_quota_time;
    }
}
